package com.xiaomi.service.impl;

import com.xiaomi.domain.OrdersEntity;
import com.xiaomi.domain.TrolleyEntity;
import com.xiaomi.domain.UserEntity;
import com.xiaomi.mappers.GoodsMapper;
import com.xiaomi.mappers.TrolleyMapper;
import com.xiaomi.util.OrderNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TrolleyServiceimpl implements com.xiaomi.service.TrolleyService {
    @Autowired
    private TrolleyMapper trolleyMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    private OrdersEntity ordersEntity;

    @Override
    public List<TrolleyEntity> Shoppingcartdisplay(Integer uid) {
        List<TrolleyEntity> shoppingcartdisplay = trolleyMapper.Shoppingcartdisplay(uid);

        for(int i=0;i<shoppingcartdisplay.size();i++){
            shoppingcartdisplay.get(i).setGoods(
                    goodsMapper.findGoodsById(String.valueOf(shoppingcartdisplay.get(i).getGid())));
        }
        return shoppingcartdisplay;
    }

    @Override
    public void addOrDeleteNumber(Integer tid, Integer number) {
        trolleyMapper.addOrDeleteNumber(tid,number);
        System.out.println(tid);
        System.out.println(number);
    }

    @Override
    public void deleteTrolley(Integer tid) {
        trolleyMapper.deleteTrolley(tid);
    }

    @Override
    public OrdersEntity createOrders(double sumPrice, Integer goodsCount, UserEntity user) throws ParseException {

       String ordersNumbers = OrderNumberUtil.Getnum();
       Integer uid = 1;
        String orderName = user.getUname()+"在"+user.getCreateTime()+"新生成的订单";


//        Date time = new Date();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        java.util.Date date=new java.util.Date();
//        String createtime=sdf.format(time);
//        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        Date createTime=new Date(format.parse(createtime).getTime());

        Date createTime = new Date();
        Integer state=0;
        ordersEntity = new OrdersEntity(ordersNumbers,uid,sumPrice,goodsCount,orderName,createTime,state);
        trolleyMapper.createOrders(ordersEntity);
        return ordersEntity;
    }

    @Override
    public void deleteByuid(int uid) {
        trolleyMapper.deleteByuid(uid);
    }

    @Override
    public int selectTrolleyCount(Integer uid) {
        return trolleyMapper.selectTrolleyCount(uid);
    }
    @Override
    public void addGoodInCart(Integer uid, Integer gid) {
        TrolleyEntity trolleyEntity = trolleyMapper.selectGidandUid(uid, gid);
        if(!(trolleyEntity==null)){
            //todo 直接改动数据
            trolleyMapper.addOrDeleteNumber(trolleyEntity.getTid(),trolleyEntity.getNumber()+1);
        }else{
            //todo 插入数据
            trolleyMapper.insertCart(uid,gid,1);
        }
    }
}
